<?php
namespace request;

class Request 
{
	private $properties;
	
	function __construct() 
	{
		$this->init();
	}

	function init() 
	{
		if ( isset( $_SERVER['REQUEST_METHOD'] ) ) 
		{
			foreach ($_REQUEST as $key => $value)
			{
				$this->setProperty( $key, $value );		
			}
		}
	}
	
	function getProperty( $key ) 
	{
		if ( isset( $this->properties[$key] ) ) 
		{
			return $this->properties[$key];
		}
		return null;
	}
	
	function setProperty( $key, $val ) 
	{
		$this->setBasicFilters($val);		
		$this->properties[$key] = $val;
	}	
	
	function setPropertyWithType( $key, $val, $type )
	{
		$this->setBasicFilters($val);
	
		$this->properties[$key] = $val;		
		settype($this->properties[$key], $type);
	}
	
	private function setBasicFilters(&$val)
	{
		if(gettype($val) != 'object')
		{
			$val = strip_tags($val);
			$val = filter_var($val, FILTER_SANITIZE_MAGIC_QUOTES);
		}
	}
	
	public function isAjax()
	{
		return !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
	}
	
	public function getMethod()
	{
		return $_SERVER['REQUEST_METHOD'];
	}
	
	public function isPost()
	{
		return $this->getMethod() == self::METHOD_POST;
	}
	
	public function isGet()
	{
		return $this->getMethod() == self::METHOD_GET;
	}
}